package com.ruoyi.dev.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dev.domain.DevModel;
import com.ruoyi.dev.service.IDevModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据模型Controller
 * 
 * @author yepanpan
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/dev/model")
public class DevModelController extends BaseController
{
    @Autowired
    private IDevModelService devModelService;

    /**
     * 查询数据模型列表
     */
    @GetMapping("/select")
    public TableDataInfo select(DevModel devModel)
    {
        List<DevModel> list = devModelService.selectDevModelList(devModel);
        return getDataTable(list);
    }

    /**
     * 查询数据模型列表
     */
    @PreAuthorize("@ss.hasPermi('dev:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(DevModel devModel)
    {
        startPage();
        List<DevModel> list = devModelService.selectDevModelList(devModel);
        return getDataTable(list);
    }

    /**
     * 导出数据模型列表
     */
    @PreAuthorize("@ss.hasPermi('dev:model:export')")
    @Log(title = "数据模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DevModel devModel)
    {
        List<DevModel> list = devModelService.selectDevModelList(devModel);
        ExcelUtil<DevModel> util = new ExcelUtil<DevModel>(DevModel.class);
        util.exportExcel(response, list, "数据模型数据");
    }

    @GetMapping("/reload")
    public AjaxResult reload()
    {
        devModelService.reCache();
        return AjaxResult.success();
    }

    /**
     * 获取数据模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(devModelService.selectDevModelById(id));
    }

    /**
     * 获取数据模型详细信息
     */
    @GetMapping(value = "detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        DevModel devModel = devModelService.selectDevModelDetail(id);
        return success(devModel);
    }

    /**
     * 新增数据模型
     */
    @PreAuthorize("@ss.hasPermi('dev:model:add')")
    @Log(title = "数据模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DevModel devModel)
    {
        return toAjax(devModelService.insertDevModel(devModel));
    }

    /**
     * 修改数据模型
     */
    @PreAuthorize("@ss.hasPermi('dev:model:edit')")
    @Log(title = "数据模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DevModel devModel)
    {
        return toAjax(devModelService.updateDevModel(devModel));
    }

    /**
     * 删除数据模型
     */
    @PreAuthorize("@ss.hasPermi('dev:model:remove')")
    @Log(title = "数据模型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(devModelService.deleteDevModelByIds(ids));
    }


    /**
     * 数据模型加到菜单
     */
    @GetMapping(value = "/menu/{id}")
    public AjaxResult addMenu(@PathVariable("id") Long id)
    {
        return success(devModelService.addDevModelMenu(id));
    }
}
